<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *活动分类管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'活动分类列表'),
	array('op'=>'edit','title'=>'添加分类'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['isshow'])){
		$params['isshow']=intval($_RQ['isshow']);
		$return['params']['isshow']=$_RQ['isshow'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','logo','isshow','banner','type','createtime');
	$list=pdo_getslice('jy_xiuvote_groups',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){

		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'分类名称','type'=>'string'),
			array('name'=>'logo','title'=>'分类封面','type'=>'image'),
			array('name'=>'type','title'=>'模式','type'=>'radio','options'=>'type'),
			array('name'=>'isshow','title'=>'是否展示','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'分类名称','type'=>'string'),
			array('name'=>'isshow','width'=>15,'title'=>'是否展示','type'=>'radio','options'=>'isshow'),
		)
	);
	$return['type']=array('0'=>'投票模式','1'=>'点赞模式');
	$return['isshow']=array('0'=>'否','1'=>'是');
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_xiuvote_groups',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['item']['banner']=json_decode($return['item']['banner'],true);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回分类列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'分类名称','type'=>'string'),
					array('name'=>'logo','title'=>'分类封面','type'=>'image'),
					array('name'=>'desc','title'=>'分类介绍','type'=>'text'),
					array('name'=>'type','title'=>'模式','type'=>'radio','options'=>'type'),
			
					array(
						'type'=>'array',
						'name'=>'banner',
						'add_tip'=>'添加一个轮播图',
						'list'=>array(
								array('type'=>'image','name'=>'picurl','title'=>'图片'),
								),
						'details'=>array(
							array('type'=>'image','name'=>'picurl','title'=>'图片'),
						)
						),
					array('name'=>'isshow','title'=>'是否显示','type'=>'switch'),
				)
			),
		)
	);
	$return['type']=array('0'=>'投票模式','1'=>'点赞模式');
	$return['htmls']=$htmls;
	//print_r($return['lev_name']);exit;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['title'])){
		exi('请完善名称','error');
	}
	if(empty($postdata['logo'])){
		exi('请选择logo','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'logo'=>trim($postdata['logo']),
		'desc'=>trim($postdata['desc']),
		'banner'=>ijson_encode($postdata['banner']),
		'isshow'=>intval($postdata['isshow']),
		'type'=>intval($postdata['type']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_xiuvote_groups',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_xiuvote_groups',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_xiuvote_groups',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的分类！','error');
	}
	$post=get_postdata();
	if(isset($post['isshow'])){
		pdo_update('jy_xiuvote_groups',array('isshow'=>intval($post['isshow'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}